package vip.finance.makemoney.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import vip.finance.makemoney.common.WebJson;
import vip.finance.makemoney.common.vo.StockRealTimeRes;
import vip.finance.makemoney.entity.Stock;
import vip.finance.makemoney.service.ETFService;
import vip.finance.makemoney.service.IStockService;
import vip.finance.makemoney.service.IUserStockService;
import vip.finance.makemoney.service.ScheduleService;

import java.util.List;

/**
 * @author 杨滨
 * @date 2023年02月01日
 */
@RestController
@RequestMapping("/stock")
public class StockController extends BaseController {
    @Resource
    private ETFService etfService;
    //    @Resource
//    private ETFFFStockService ETFFFStockService;
    @Resource
    private IStockService stockService;
    @Resource
    private IUserStockService userStockService;
    @Resource
    private ScheduleService scheduleService;

    //股票搜索
    @GetMapping("/search")
    public WebJson<Object> search(@RequestParam String content) {
        content = content.trim();
        List<Stock> stockList = stockService.list(Wrappers.<Stock>lambdaQuery()
                .like(Stock::getCode, content)
                .or().like(Stock::getName, content)
                .or().like(Stock::getShortCode, content).last("limit 10"));
        return WebJson.success(stockList);
    }

    //股票列表
    @GetMapping("/stock-page-list")
    public WebJson<IPage<Stock>> stockPageList(@RequestParam Integer size, @RequestParam Integer current, @RequestParam String market) {
        return WebJson.success(stockService.stockPageList(current, size, market));
    }

    //添加自选
    @PostMapping("/user-stock-add")
    public WebJson<Object> add(@RequestParam String stockCode) {
        userStockService.addUserStock(currentUser().getId(), stockCode);
        return WebJson.success();
    }

    //我的自选
    @GetMapping("/user-stock-list")
    public WebJson<List<StockRealTimeRes>> list() {
        return WebJson.success(userStockService.listRealTime(currentUser().getId()));
    }

    //我的自选
    @PostMapping("/update-user-stocks")
    public WebJson<List<StockRealTimeRes>> updateUserStocks(@RequestBody List<StockRealTimeRes> realTimeResList) {
        userStockService.updateUserStocks(currentUser().getId(), realTimeResList);
        return WebJson.success();
    }

//    //查询ETF成分明细
//    @NoAuth
//    @GetMapping("/etf/detail")
//    public WebJson<Object> etfDetail(String code) {
//        return WebJson.success(ETFFFStockService.etfDetail(code));
//    }
}
